﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.CommonPage
{
    public partial class studentslist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                binddll();
                bind("1=1");
            }
        }

        void bindClass(string Year, string Pro)
        {
            List<LabMS.Model.Class> listClass = new List<LabMS.Model.Class>();
            LabMS.BLL.Class bClass = new LabMS.BLL.Class();

            string strWhere = "1=1";
            if (Year != "0")
            {
                strWhere += " and  Class_Year ='" + Year + "'";
            }
            if (Pro!="0")
            {
                strWhere += " and  Class_Professional =" + Pro;
            }

            listClass = bClass.GetModelList(strWhere);
            ddlClass.DataSource = listClass;
            ddlClass.DataValueField = "ID";
            ddlClass.DataTextField = "Class_Name";
            ddlClass.DataBind();
            ddlClass.Items.Insert(0, new ListItem("", "0"));
        }

        void binddll()
        {
            LabMS.Model.SysSetting msys = new LabMS.Model.SysSetting();
            ExtendBLL.SysSetting extendsys = new ExtendBLL.SysSetting();
            msys = extendsys.GetCurrentSetting();
            List<string> listYear = Common.GradeHelper.GetCurrentGrade((int)msys.CurrentYear, (int)msys.CurrentSemester, false);
            ddlYear.DataSource = listYear;
            ddlYear.DataBind();
            ddlYear.Items.Insert(0, new ListItem("", "0"));

            List<LabMS.Model.Professional> listPro = new List<LabMS.Model.Professional>();
            LabMS.BLL.Professional bPro = new LabMS.BLL.Professional();
            listPro = bPro.GetModelList("");
            ddlPro.DataSource = listPro;
            ddlPro.DataValueField = "ID";
            ddlPro.DataTextField = "Pro_Name";
            ddlPro.DataBind();
            ddlPro.Items.Insert(0, new ListItem("", "0"));
            bindClass(ddlYear.SelectedValue, ddlPro.SelectedValue);
        }

        public void bind(string strWhere)
        {
            List<LabMS.Model.Student> liststudentsmodule = new
                List<LabMS.Model.Student>();
            LabMS.BLL.Student bstudent = new LabMS.BLL.Student();
            liststudentsmodule = bstudent.GetModelList(strWhere + " order by ID desc ");
            gvStudents.DataSource = liststudentsmodule;
            gvStudents.DataBind();
        }
        protected void gvStudents_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvStudents.PageIndex = e.NewPageIndex;
            if (lbboolSearch.Text != "1")
            {
                bind("1=1");
            }
            else
            {
                searchbind();
            }
        }

        protected void gvStudents_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes["style"] = "Cursor:pointer"; 
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            searchbind();
            lbboolSearch.Text = "1";
        }

        void searchbind()
        {
            string strwhere = " 1=1 ";
            string strclass = ddlClass.SelectedValue;

            string strYear = ddlYear.SelectedValue;
            string strPro = ddlPro.SelectedValue;
            string strSName = tbSName.Text.Trim();
            string strSCode = tbSNub.Text.Trim();
            if (strclass != "0")
            {
                strwhere += " and ClassID = " + strclass;
            }
            if (strYear != "0")
            {
                List<LabMS.Model.Class> listClass = new List<LabMS.Model.Class>();
                LabMS.BLL.Class bclass = new LabMS.BLL.Class();
                bclass.GetModelList("Class_Year = '"+ strYear +"'");

                strwhere += " and ( 1<>1  ";

                foreach (LabMS.Model.Class c in listClass)
                {
                    strwhere += " and ( or ClassID = " + c.ID.ToString();
                }
                strwhere += ")";
            }
            if (strPro != "0")
            {
                strwhere += " and Student_Pro = " + strPro;
            }
            if (strSCode != "")
            {
                strwhere += " and Student_Code like '%" + strSCode + "%'";
            }
            if (strSName != "")
            {
                strwhere += " and Student_Name like '%" + strSName + "%'";
            }
            bind(strwhere);
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindClass(ddlYear.SelectedValue, ddlPro.SelectedValue);
        }

        ////protected void btOK_Click(object sender, EventArgs e)
        //{
        //    CheckBox ch;
        //    LabMS.BLL.Student bstudent = new LabMS.BLL.Student();
        //    List<LabMS.Model.Student> liststudentsmodule = new
        //       List<LabMS.Model.Student>();
        //    LabMS.Model.Student studentsmodule = new
        //       LabMS.Model.Student();
        //    if (Session["Studentlist"] != null)
        //    {
        //        liststudentsmodule = (List<LabMS.Model.Student>)Session["Studentlist"];
        //    }
        //    try
        //    {
        //        for (int i = 0; i < gvStudents.Rows.Count; i++)
        //        {
        //            ch = (CheckBox)gvStudents.Rows[i].FindControl("checkdel");
        //            if (ch.Checked)
        //            {
        //                string CID = gvStudents.DataKeys[i].Value.ToString();
        //                studentsmodule = bstudent.GetModel(int.Parse(CID));
        //                if (liststudentsmodule.Count == 0)
        //                {
        //                    liststudentsmodule.Add(studentsmodule);
        //                }
        //                else
        //                {
        //                    bool isSame = false;
        //                    foreach (LabMS.Model.Student s in liststudentsmodule)
        //                    {
        //                        if (s.ID == studentsmodule.ID)
        //                        {
        //                            isSame = true;
        //                            break;
        //                        }
        //                    }
        //                    if (!isSame)
        //                    {
        //                        liststudentsmodule.Add(studentsmodule);
        //                    }
        //                }
                        
        //            }
        //        }
        //        Session["Studentlist"] = liststudentsmodule;

        //        Page.RegisterStartupScript("success", "<script>alert(\"添加成功\");window.opener.document.location.href='../OpenProject/addopenproject.aspx';</script>");
        //    }
        //    catch (Exception ex)
        //    {
        //        Common.JShelper.JSAlert(Page, "err", "添加失败!");
        //    }
        //}
    }
}
